package com.connect.nio;


import com.dto.cnc.CncDTO;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.scheduling.annotation.EnableAsync;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

@EnableAsync
@AllArgsConstructor
@NoArgsConstructor
public class EchoClient {

    private String host = CncDTO.getCnc_ip();
    private int port = Integer.parseInt(CncDTO.getCnc_port());
    private String str;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start(EventLoopGroup group, EchoClient echoHandler) throws Exception {
//        group = new NioEventLoopGroup(2,new DefaultThreadFactory("server",false));

        try {

            Bootstrap bootstrap = new Bootstrap();
            // 连接超时时间
            bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
            // 心跳
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            // 配置Channel参数，nodelay没有延迟，true就代表禁用Nagle算法，减小传输延迟。
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            /*立即释放*/
            bootstrap.option(ChannelOption.SO_REUSEADDR, true);

            bootstrap.group(group) // 注册线程池
                    .channel(NioSocketChannel.class) // 使用NioSocketChannel来作为连接用的channel类
                    .remoteAddress(new InetSocketAddress(this.host, this.port)) // 绑定连接端口和host信息
                    .handler(new ChannelInitializer<SocketChannel>() { // 绑定连接初始化器
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //System.out.println("正在连接中...");
                            ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
                            ch.pipeline().addLast((ChannelHandler) echoHandler);/*返回结果*/
                            ch.pipeline().addLast(new ByteArrayEncoder());
                            ch.pipeline().addLast(new ChunkedWriteHandler());

                        }
                    });
            // System.out.println("服务端连接成功..");

            ChannelFuture cf = bootstrap.connect().sync(); // 异步连接服务器
            //System.out.println("服务端连接成功..."); // 连接完成




            cf.channel().closeFuture().sync(); // 异步等待关闭连接channel
           // System.out.println("连接已关闭.."); // 关闭完成

        } finally {

            group.shutdownGracefully(); // 释放线程池资源
//            return Notify_Alarm.getV1();
        }

    }




//    public static void main(String[] args) throws Exception {
//        new EchoClient("10.2.52.10", 62937).start(); // 连接127.0.0.1/65535，并启动
//
//    }
}
